package com.lagou.controller;/**
 * Created by IntelliJ IDEA.
 * User: Yangdx.
 * Date: 2020/7/25.
 * Time: 3:39.
 * Tags: Code, we are serious.
 */

import com.lagou.service.AuthCodeService;
import com.lagou.service.EmailServiceClient;
import com.lagou.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @create 2020-07-25 3:39
 **/
@RestController
@RequestMapping("/code")
public class AuthCodeController {
    @Autowired
    private AuthCodeService authCodeService;

    @Autowired
    private EmailServiceClient emailServiceClient;

    @Value("${config.code.length}")
    private int codeLength;

    @GetMapping("/create/{email}")
    public boolean createAuthCode(@PathVariable("email") String email) {
        String code = RandomUtil.getCode(codeLength);
        authCodeService.createNewCode(email, code);
        return emailServiceClient.sendMail(email, code);
    }

    @GetMapping("/validate/{email}/{code}")
    public int validateAuthCode(@PathVariable("email") String email, @PathVariable("code") String code) {
        return authCodeService.validateAuthCode(email, code).ordinal();
    }
}
